package com.grape.api;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import com.grape.annotation.OperateMethodName;
import com.grape.controller.BaseController;
import com.grape.result.R;
import com.grape.vo.BaseVo;
import com.grape.domain.SysDictItem;
import com.grape.service.ISysDictItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 字典数据表 控制器
 * @author  putao
 * @email   2628279194@qq.com
 */
@RestController
@RequestMapping("dictItems")
@Api(tags = "字典数据接口")
@AllArgsConstructor
public class SysDictItemController extends BaseController<SysDictItem> {

    private final ISysDictItemService sysDictItemService;

    @GetMapping
    @ApiOperation(value = "字典数据列表")
    @OperateMethodName(value = "字典数据表列表", url = "system:dictItems:listPage")
    public R<?> listPage(BaseVo baseVo) {
        PageInfo<SysDictItem> pageInfo = sysDictItemService.pageListByDelFlag(baseVo);
        List<SysDictItem> sysDictItemList = pageInfo.getList();
        return CollectionUtil.isNotEmpty(sysDictItemList) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }

    @PutMapping
    @ApiOperation(value = "字典数据修改")
    @OperateMethodName(value = "字典数据表修改", url = "system:dictItems:update")
    public R<?> update(@Validated @RequestBody SysDictItem sysDictItem) {
        if (!Strings.isNotBlank(sysDictItem.getId())){
            return updateNotFound(sysDictItem, null);
        }
        sysDictItemService.updateData(sysDictItem);
        return updateSuccess(null, null);
    }


    @PostMapping
    @ApiOperation(value = "添加字典数据")
    @OperateMethodName(value = "添加字典数据表", url = "system:dictItems:insert")
    public R<?> insert(@Validated @RequestBody SysDictItem sysDictItem) {
        return insertSuccess(sysDictItemService.insertData(sysDictItem), null);
    }

    @DeleteMapping
    @ApiOperation(value = "删除字典数据，可批量多个id逗号拼接")
    @OperateMethodName(value = "字典数据表删除,批量", url = "system:dictItems:delete")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空")  @RequestParam String ids) {
        int number = sysDictItemService.deleteData(ids);
        if (number < 0) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }

    @GetMapping("detail")
    @ApiOperation(value = "字典数据详情")
    @OperateMethodName(value = "字典数据表详情", url = "system:dictItems:detail")
    public R<?> detail(@NotBlank(message = "详情时id不能为空") String id) {
        SysDictItem sysDictItem = sysDictItemService.detail(id);
        if (sysDictItem != null) {
            return selectSuccess(sysDictItem, id);
        }
        return selectNotFound(id, null);
    }
}

